// $Id: assert.h 9 2008-09-30 03:21:47Z rodlima $

/*
   Copyright (c) 2008, Rodolfo Schulz de Lima 
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.  
   * Neither the name of RodSoft nor the names of its contributors may be used
     to endorse or promote products derived from this software without specific
     prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ORM_ASSERT_H
#define ORM_ASSERT_H

namespace orm
{

typedef void (*assert_fun_t)(const char *);

void set_assert_func(assert_fun_t fun);
void set_assert_progname(const char *progname);

enum tipo_assert
{
    SHOW_MSG=1,
    ABORT=2,
    BACKTRACE=4
};

void set_assert_type(unsigned tipo = SHOW_MSG|ABORT);

namespace detail/*{{{*/
{
    const char *cria_msg_assert(const char *msg, 
				const char *arq,
				unsigned linha,
				const char *func, 
				const char *e=0);

    void show_assert(const char *msg);
    
    template <class E> void _assert_fail(const char *msg)/*{{{*/
    {
#ifndef NDEBUG
        detail::show_assert(msg);
#endif
        throw E(msg);
    }/*}}}*/
    inline bool _assert_fail(const char *msg)/*{{{*/
    {
#ifndef NDEBUG
        detail::show_assert(msg);
#endif
        return false;
    }/*}}}*/

}/*}}}*/

#define ASSERT_STRING(x,e) ::orm::detail::cria_msg_assert(x,__FILE__, __LINE__, __PRETTY_FUNCTION__,e)

#define cassert(x) (!(x)?::orm::detail::_assert_fail( \
	ASSERT_STRING( #x , 0)):true)
#define eassert(x, e) if(!(x)) ::orm::detail::_assert_fail<e>( \
	ASSERT_STRING( #x , #e))

#ifdef assert
#undef assert
#endif

#ifdef NDEBUG
#define assert(x) static_cast<void>(0)
#else
#define assert(x) cassert(x)
#endif
										
} // namespace orm


#endif

